/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
/*                                                                         */
/* Redistribution and use in source and binary forms, with or without      */
/* modification, are permitted provided that the following conditions      */
/* are met:                                                                */
/*                                                                         */
/*    1. Redistributions of source code must retain the above copyright    */
/* notice, this list of conditions and the following disclaimer.           */
/*    2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the     */
/* documentation and/or other materials provided with the distribution.    */
/*                                                                         */
/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* sections apply to the below license                                     */
/* certain parts of the http request code are copied or adapted from       */
/* serf_get.c in the apr-serf/test/ dir                                    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 */

#include "athd.h"

/* privates */
/* static apr_status_t print_bucket(apr_bucket *bucket,  */
/* 				 apr_file_t *file, apr_size_t *read); */


apr_status_t
athd_filter_header_host(apr_bucket_brigade *brigade,
			     serf_filter_t *filter,
			     apr_pool_t *pool)
{
    apr_bucket *bucket;
    serf_request_t *request = filter->ctx;
    bucket = serf_bucket_header_create("Host",
				       request->uri->hostname,
				       pool, 
				       brigade->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(brigade, bucket);
    return APR_SUCCESS;
}

apr_status_t
athd_filter_header_agent(apr_bucket_brigade *brigade,
			      serf_filter_t *filter,
			      apr_pool_t *pool)
{
    apr_bucket *bucket;
    serf_request_t *request = filter->ctx;
    bucket = serf_bucket_header_create("User-Agent",
				       ATHD_USER_AGENT,
				       pool,
				       brigade->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(brigade, bucket);
    return APR_SUCCESS;
}


apr_status_t 
athd_filter_http_headers(apr_bucket_brigade *brigade,
			      serf_filter_t *filter,
			      apr_pool_t *pool)
{
    apr_bucket *bucket;
    /* All we do here is stick CRLFs in the right places. */
    bucket = APR_BRIGADE_FIRST(brigade);
    while (bucket != APR_BRIGADE_SENTINEL(brigade)) {
        if (SERF_BUCKET_IS_REQUEST_LINE(bucket) ||
            SERF_BUCKET_IS_HEADER(bucket)) {
            apr_bucket *eol;
	    
            eol = apr_bucket_immortal_create(CRLF, sizeof(CRLF)-1,
                                             brigade->bucket_alloc);

            APR_BUCKET_INSERT_AFTER(bucket, eol);
	}
	
        bucket = APR_BUCKET_NEXT(bucket);
    }
    
    /* FIXME: We need a way to indicate we are EOH. */
    bucket = apr_bucket_immortal_create(CRLF, sizeof(CRLF)-1,
                                        brigade->bucket_alloc);
    APR_BRIGADE_INSERT_TAIL(brigade, bucket);
    return APR_SUCCESS;
}

/* we run this filter so keep-alive logic knows how much data to read in */
apr_status_t
athd_filter_resp_len(apr_bucket_brigade *brigade, serf_filter_t *filter,
			  apr_pool_t *pool)
{
    apr_status_t rv = APR_SUCCESS;
    apr_bucket *bucket;
    athd_request *req;    

    ASSERT(filter->ctx);
    req = filter->ctx;

    for (bucket = APR_BRIGADE_FIRST(brigade);
         bucket != APR_BRIGADE_SENTINEL(brigade);
         bucket = APR_BUCKET_NEXT(bucket)) 
      {
        if (SERF_BUCKET_IS_HEADER(bucket)) 
	  {
	      apr_size_t length;   
	      const char *buf;
	      rv = apr_bucket_read(bucket, &buf, &length, APR_BLOCK_READ); 
	      if ((!rv)
		  && (!strncasecmp("Content-length: ", buf, 14)))
		{
		    char *tok, *intern;
		    char *sep = " ";
		    char *src = apr_pstrdup(req->pool, buf);
		    tok = apr_strtok(src, sep, &intern);
		    tok = apr_strtok(NULL, sep, &intern);
		    /* atoi is ok. our response is small */
		    if (tok)
			req->content_length = atoi(tok);
		}
	  } 
      }
    return rv;
}

/* static apr_status_t  */
/* print_bucket(apr_bucket *bucket, apr_file_t *file, apr_size_t *length) */
/* { */
/*     const char *buf; */
/*     apr_size_t written; */
/*     apr_status_t rv; */
/*     rv = apr_bucket_read(bucket, &buf, length, APR_BLOCK_READ); */
/*     RV_OK = apr_file_write_full(file, buf, (*length), &written); */
/*     return rv; */
/* } */
